<template>
  <div class="building">
    <view-header :title="'楼宇管理'"></view-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="6">
            <el-form-item label="宿舍楼名：">
              <el-input type="text" v-model="queryForm.buildingName" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="宿管名：">
              <el-input type="text" v-model="queryForm.type" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否住满：">
              <el-select v-model="queryForm.fullFlag" placeholder="请选择" style="width: 100%;" clearable>
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="6">
          <div class="btn_area">
            <el-button class="confirm" @click="queryBuildList">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confirm" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增宿舍楼信息': '编辑宿舍楼信息'" :visible.sync="visible" @close="close" width="60%">
      <el-row>
        <el-form :form="buildForm" label-width="1.4rem">
          <el-col :span="11">
            <el-form-item label="宿舍楼名称：" required>
              <el-input v-model="buildForm.buildingName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="层数：" required>
              <el-input :disabled="code == '2'" v-model="buildForm.pliesNumber" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="每层宿舍数：" required>
              <el-input :disabled="code == '2'" v-model="buildForm.quartersNumber" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="宿舍人数：" required>
              <el-input :disabled="code == '2'" v-model="buildForm.peopleNumber" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="男/女宿舍楼：" required>
              <el-radio-group v-model="buildForm.buildingGender" @change="genderChange">
                <el-radio label="1">男</el-radio>
                <el-radio label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="宿舍管理人员：" required>
              <el-select v-model="buildForm.staffId" placeholder="请选择宿舍管理员" style="width: 100%;">
                <el-option v-for="item in stafflist" :key="item.uuid" :label="item.userName" :disabled="item.disabled == '1'" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="宿舍图片：" required>
              <el-image
                v-if="buildForm.buildingPicture" 
                style="width: 50%; height: 150px"
                :src="buildForm.buildingPicture" 
                :preview-src-list="[buildForm.buildingPicture]"
                fit="scale-down">
              </el-image>
              <span @click="openPhoto" v-if="buildForm.buildingPicture" style="margin-left: 0.2rem;font-size: 0.18rem;cursor: pointer; text-decoration: underline; color: #9c88ff;user-select: none;">修改</span>
              <span @click="openPhoto" v-else style="margin-left: 0.2rem;font-size: 0.18rem;cursor: pointer; text-decoration: underline; color: #9c88ff;user-select: none;">暂无图片，点击上传</span>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confirm" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="上传图片" :visible.sync="photoVisible" width="35%" :before-close="close1" :modal="false" :append-to-body="false" class="xiugaimima dialog_box">
      <el-upload ref="upload" class="avatar-uploader" action="" :show-file-list="false" :auto-upload="false"
        :on-change="updatachange" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button class="confirm" @click="uploadImg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import viewHeader from '@/components/viewHeader.vue'
export default {
  name: 'building',
  components: {
    tableList,
    viewHeader
  },
  data() {
    return {
      photoVisible: false,
      imageUrl: "",
      imageHead: "",
      queryForm: {
        buildingName: '', // 宿舍楼名
        fullFlag: '', // 是否已住满
        userName: '' // 宿管名
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'buildingName',
          label: '宿舍楼名',
          align: 'center'
        },
        {
          prop: 'userName',
          label: '宿管管理员',
          align: 'center'
        },
        {
          prop: 'buildingGenderName',
          label: '男/女宿舍楼',
          align: 'center'
        },
        {
          prop: 'totalSpace',
          label: '总共可住人数',
          align: 'center'
        },
        {
          prop: 'occupancy',
          label: '已住人数',
          align: 'center'
        },
        {
          prop: 'freeSpace',
          label: '剩余空间',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'danger',desc:'删除'},
            {type: 'success',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      user: {},
      visible: false,
      stafflist: [],
      infostafflist: [],
      code: '',
      buildForm: {
        staffId: ""
      },
      url: 'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
      imgList: ['https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg']
    }
  },
  methods: {
    genderChange(val) {
      console.log(val);
      this.stafflist = [];
      this.buildForm.staffId = "";
      this.stafflist = this.infostafflist.filter(item => item.gender == val);
      console.log(this.stafflist);
    },
    openPhoto() {
      if (this.$refs.upload) {
        this.$refs.upload.clearFiles();
        this.imageUrl = "";
        this.imageHead = "";
      }
      this.photoVisible = true;
    },
    uploadImg() {
      let url = this.$api.common.photo;
      let fileFormData = new FormData();
      fileFormData.append("file", this.imageHead);
      this.$http(url, fileFormData, "", true).then((res) => {
        if (res.data.code === 200) {
          this.buildForm.buildingPicture = res.data.data
          this.photoVisible = false;
        } else {
          this.$message({
            message: res.data.message,
            type: "error",
            duration: 2000,
          });
        }
      });
    },
    updatachange(res) {
      this.imageUrl = URL.createObjectURL(res.raw);
      this.imageHead = res.raw;
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png";
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      return isJPG;
    },
    confirm() {
      console.log(this.buildForm);
      let url = this.code == '1' ? this.$api.build.create : this.$api.build.update
      console.log(this.buildForm);
      this.$http(url,this.reqData(this.buildForm)).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryBuildList();
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    close() {
      this.buildForm = {
        staffId: ""
      };
    },
    close1() {
      this.photoVisible = false;
    },
    add() {
      this.queryStaffList();
      this.code = '1';
      this.visible = true;
    },
    reset() {
      this.queryForm = {
        buildingName: '', // 宿舍楼名
        fullFlag: '', // 是否已住满
        userName: '' // 宿管名
      }
      this.queryBuildList();
    },
    onHandle(data) {
      let {row,type} = data;
      if (type == 'danger') {
        this.$http(this.$api.build.delete,this.reqData(row.uuid)).then(res => {
          if (res.data.code == 200) {
            this.$message.success(res.data.message)
            this.queryBuildList();
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else {
        this.code = '2'
        this.queryStaffList()
        this.$nextTick(() => {
          this.$http(this.$api.build.info,this.reqData(row.uuid)).then(res => {
            if (res.data.code == 200) {
              this.buildForm = res.data.data
              this.visible = true;
              this.stafflist = [];
              this.stafflist = this.infostafflist.filter(item => item.gender == this.buildForm.buildingGender);
              this.$forceUpdate()
              console.log(this.buildForm);
            } else {
              this.$message.error(res.data.message)
            }
          })
        })
      }
      
      
    },
    queryBuildList() {
      this.$http(this.$api.build.list,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data;
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
            item.buildingGenderName = item.buildingGender == '0' ? '女宿舍楼' : '男宿舍楼'
          })
          this.total = this.list.length;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryBuildList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryBuildList();
    },
    queryStaffList() {
      this.$http(this.$api.user.stafflist).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data;
          this.infostafflist = data
          console.log(data);
        }
      })
    }
  },
  mounted() {
    this.user = JSON.parse(sessionStorage.getItem('user'));
    this.queryBuildList();
  }
}
</script>
<style lang="less" scoped>
.building {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
  /deep/ .xiugaimima {
    .el-dialog {
      .el-dialog__body {
        padding: 0.1rem 0 0.1rem 0;
        /*padding-left: 0 !important;*/
        display: flex;
        justify-content: center;

        .el-form {
          width: 80%;

          .el-form-item {
            margin: 0 !important;
            margin-top: 0.25rem !important;
            .el-form-item__label {
              font-size: 0.18rem !important;
            }
          }
        }
      }

      .el-dialog__footer {
        text-align: center;
      }
    }
  }

  .xiugaimima {
    /deep/ .avatar-uploader .el-upload {
      border: 1px solid #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }

    /deep/ .avatar-uploader .el-upload:hover {
      border-color: #9c88ff;
    }

    /deep/ .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    /deep/ .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>
<style lang="less">
.building {
  
}

</style>